package com.shop.shoporder.service.impl;

import com.shop.shoporder.entity.order.Freight;
import com.shop.shoporder.entity.order.FreightData;
import com.shop.shoporder.mapper.order.FreightMapper;
import com.shop.shoporder.service.FreightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("freightService")
public class FreightServiceImpl implements FreightService {
    @Autowired
    FreightMapper freightMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return freightMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Freight record) {
        return freightMapper.insert(record);
    }

    @Override
    public int insertSelective(Freight record) {
        return freightMapper.insertSelective(record);
    }

    @Override
    public Freight selectByPrimaryKey(Integer id) {
        return freightMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Freight record) {
        return freightMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Freight record) {
        return freightMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Freight> selectAll() {
        return freightMapper.selectAll();
    }

    @Override
    public Freight selectFreight(Double weight, String ids) {
        return freightMapper.selectFreight(weight,ids);
    }

    @Override
    public List<Freight> selectFreight1(String ids) {
        return freightMapper.selectFreight1(ids);
    }

    @Override
    public Double getFreight(List<FreightData> freightDataList){
        long start = System.currentTimeMillis();
        List<Freight> listX=freightMapper.selectAll();//所有基础运费信息
        double freightDouble=0,freightDouble1=0;
        List<Freight> list1=listX.stream().filter(c ->(c.getGuigeId()!=null)).collect(Collectors.toList());
        List<FreightData> freightDatax=freightDataList;
        for (int lm = 0; lm <freightDataList.size() ; lm++) {
            FreightData freightDataX=freightDataList.get(lm);
            int guigeId=freightDataX.getGuigeId()==null?-1:freightDataX.getGuigeId();
            int number=freightDataX.getNumber()==null?0:freightDataX.getNumber();
            String provinceId=freightDataX.getProvinceId()==null?"0":freightDataX.getProvinceId().toString();
            for (int lk = 0; lk <list1.size() ; lk++) {
                Freight freightX=list1.get(lk);
                String areaIds=freightX.getAreaids();
                int areaNo=freightX.getAreano()==null?0:freightX.getAreano();
                int guigeIdF=freightX.getGuigeId()==null?0:freightX.getGuigeId();
                int increment=freightX.getIncrement()==null?0:freightX.getIncrement();
                double constant = freightX.getConstant() == null ? 0 : freightX.getConstant();
                if(checkArea(provinceId,areaIds)==true&&guigeId==guigeIdF&&areaNo<0){
                    freightDouble +=increment*number;
                    freightDatax.remove(freightDataX);
                    break;
                }
                if(checkArea(provinceId,areaIds)==false&&guigeId==guigeIdF&&areaNo<0){
                    freightDouble +=constant*number;
                    freightDatax.remove(freightDataX);
                    break;
                }
                if(checkArea(provinceId,areaIds)==false&&guigeId==guigeIdF&&areaNo!=0){
                    freightDouble +=increment*number;
                    freightDatax.remove(freightDataX);
                    break;
                }
            }
        }
        for (int h = 0; h <freightDatax.size() ; h++) {
            FreightData freightDataX=freightDatax.get(h);
            int guigeId=freightDataX.getGuigeId()==null?0:freightDataX.getGuigeId();
            double weight=freightDataX.getWeight()==null?0:freightDataX.getWeight();
            int number=freightDataX.getNumber()==null?0:freightDataX.getNumber();
            double weightAll=freightDataX.getWeightAll()==null?0:freightDataX.getWeightAll();
            String provinceId=freightDataX.getProvinceId()==null?"0":freightDataX.getProvinceId().toString();
            for (int v = 0; v <listX.size() ; v++) {
                Freight freightX=listX.get(v);
                String areaIds=freightX.getAreaids();
                int areaNo=freightX.getAreano()==null?0:freightX.getAreano();
                int guigeIdF=freightX.getGuigeId()==null?0:freightX.getGuigeId();
                int increment=freightX.getIncrement()==null?0:freightX.getIncrement();
                int notFirstWeightOne = freightX.getNotFirstWeightOne() == null ? 0 : freightX.getNotFirstWeightOne();
                int notFirstWeight = freightX.getNotFirstWeight() == null ? 0 : freightX.getNotFirstWeight();
                double notFirstWeightPrice = freightX.getNotFirstWeightPrice() == null ? 0 : freightX.getNotFirstWeightPrice();
                int firstWeight = freightX.getFirstWeight() == null ? 0 : freightX.getFirstWeight();
                double constant = freightX.getConstant() == null ? 0 : freightX.getConstant();
                //System.out.println("guigeId:"+guigeId+" guigeIdF:"+guigeIdF+" bool:"+checkArea(provinceId,areaIds)+" areaNo:"+areaNo);
                if(checkArea(provinceId,areaIds)==true&&areaNo==0&&guigeId==guigeIdF){
                    freightDouble +=number*increment;
                    break;
                }else if(checkArea(provinceId,areaIds)==true&&areaNo==4){//特殊地区收取运费 4区
                    Double weightInt = Math.ceil(weight*number);//向上取整 ex：15.01为16
                    freightDouble +=weightInt*increment;
                    break;
                }else if (checkArea(provinceId,areaIds)==true&&notFirstWeightOne < weightAll && notFirstWeight >= weightAll) {
                    freightDouble += notFirstWeightPrice;
                    break;
                }else if (checkArea(provinceId,areaIds)==true&&weightAll > firstWeight && firstWeight > 0) {

                    Double weightInt = Math.ceil(weightAll);//向上取整 ex：15.01为16
                    freightDouble += notFirstWeightPrice + (increment * (weightInt - firstWeight)) + constant;
                    break;
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("运费完成，耗时：" + (end - start) +" ms");
        return freightDouble;
    }

    @Override
    public Boolean checkArea(String provinceId, String areaIds) {
        boolean bools=false;
        String []strs=areaIds.split(",");
        for (String str:strs){
            if(str.equals(provinceId)){
                bools=true;
                break;
            }
        }
        return bools;
    }
}
